package database;

// <editor-fold defaultstate="collapsed" desc="HeaderFiles">
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;// </editor-fold>

/**
 * @version
 *
 * @Date Mar 24, 2011
 * @author Rajeeb Saha
 *
 */
public class ConnectionDAO {

    // <editor-fold defaultstate="collapsed" desc="Private Variables">
    private Connection conn = null;
    private Statement sqlStatement;
    private static final Logger logger =
            Logger.getLogger(ConnectionDAO.class.getName());// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructor">
    /**
     *
     */
    public ConnectionDAO() {
        String userName = "root";
        String password = "766937";
        String url = "jdbc:mysql://localhost/digistore";

        setConn(userName, password, url);
        System.out.println("connected");
    }

    /**
     *
     * @param userName
     * @param password
     * @param url
     */
    public ConnectionDAO(String userName, String password, String url) {
        setConn(userName, password, url);
    }

    /**
     *
     * @param conn
     * @param sqlStatement
     */
    public ConnectionDAO(Connection conn, Statement sqlStatement) {
        this.conn = conn;
        this.sqlStatement = sqlStatement;
    }
    // </editor-fold>

    /**
     * @return the conn
     */
    protected Connection getConn() {
        return conn;
    }

    /**
     * set database connection
     * @param userName
     * @param password
     * @param url
     */
    protected void setConn(String userName, String password, String url) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);
            sqlStatement = conn.createStatement(); //create a statement that we can use later
            conn.setAutoCommit(false); //explicitly commit each transaction
            //     System.out.println("Database connection established");
        } catch (ClassNotFoundException ex) {
            logger.log(Level.SEVERE, null, "connectionDAO : setConn" + ex);
        } catch (InstantiationException ex) {
            logger.log(Level.SEVERE, null, "connectionDAO : setConn" + ex);
        } catch (IllegalAccessException ex) {
            logger.log(Level.SEVERE, null, "connectionDAO : setConn" + ex);
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "connectionDAO : setConn" + ex);
        }
    }

    /**
     * @return the sqlStatement
     */
    protected Statement getSqlStatement() {
        return sqlStatement;
    }

    /**
     * disconnect database connection 
     */
    protected void disconnectDB() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                logger.log(Level.SEVERE, null, "connectionDAO : disconnectDB" +
                        ex);
            }
        }
    }
}
